$( "#ulNotification>li").each(function( index ) {
	//console.log( index + ": " + this.id );
	notifications.push(this.id);
});

function changeReadOrUnread(){
	if(quantityNotificationFriendshipUnRead >0){
		$('#notificationFriend').css('background-image', 'url("' + urlImg + 'iconNotificationFriendshipUnread.png")');

		var c = document.getElementById("divQuantityNotificationFriendshipUnRead");
		var ctx = c.getContext("2d");
		ctx.fillStyle = "Red";
		ctx.font = "600 10px Arial";
		ctx.textAlign = "right";
		ctx.clearRect(0, 0, 20, 10);
		ctx.fillText(quantityNotificationFriendshipUnRead,20,10);
	};

	if(quantityNotificationMessengerUnRead >0){
		$('#notificationMessage').css('background-image', 'url("' + urlImg + 'iconNotificationMessengerUnread.png")');

		var c = document.getElementById("divQuantityNotificationMessengerUnRead");
		var ctx = c.getContext("2d");
		ctx.fillStyle = "Red";
		ctx.font = "600 10px Arial";
		ctx.textAlign = "right";
		ctx.clearRect(0, 0, 20, 10);
		ctx.fillText(quantityNotificationMessengerUnRead,20,10);
	};

	if(quantityNotificationNewsUnRead >0){
		$('#notificationNews').css('background-image', 'url("' + urlImg + 'iconNotificationNewsUnread.png")');

		var c = document.getElementById("divQuantityNotificationNewsUnRead");
		var ctx = c.getContext("2d");
		ctx.fillStyle = "Red";
		ctx.font = "600 10px Arial";
		ctx.textAlign = "right";
		ctx.clearRect(0, 0, 20, 10);
		ctx.fillText(quantityNotificationNewsUnRead,20,10);
	};
}
changeReadOrUnread();
$(document).mousedown(function(e) {
    switch (e.which) {
        case 1:
            //alert('Left Mouse button pressed.');
            var target = e.target;

			//console.log(target);

			if(jQuery.inArray(target.id, notifications)!==-1) {
			    //console.log("is in array");
			    if(currentNotification != ''){
					$(currentNotification).fadeOut();
					currentNotification = '';
				}

				currentNotification = "#"+target.id +" ul";
				var status = $(currentNotification).css("display");
				if(status == 'none'){
					$(currentNotification).fadeIn();
					//Nếu id click là notificationMessage thì không gọi hàm đã đọc tất cả
					if(target.id != 'notificationMessage'){
						//Call function allRead
						switch (target.id){
							case 'notificationFriend':
								if(quantityNotificationFriendshipUnRead > 0){
									$('#notificationFriend').css('background-image', 'url("' + urlImg + 'iconNotificationFriendshipRead.png")');
									var c = document.getElementById("divQuantityNotificationFriendshipUnRead");
									var ctx = c.getContext("2d");
									ctx.clearRect(0, 0, 20, 10);
									quantityNotificationFriendshipUnRead = 0;

									ajaxReadNotification(target.id,'');
								}
								break;
							case 'notificationNews':
								if(quantityNotificationNewsUnRead > 0){
									$('#notificationNews').css('background-image', 'url("' + urlImg + 'iconNotificationNewsRead.png")');
									var c = document.getElementById("divQuantityNotificationNewsUnRead");
									var ctx = c.getContext("2d");
									ctx.clearRect(0, 0, 20, 10);
									quantityNotificationNewsUnRead = 0;

									ajaxReadNotification(target.id,'');
								}
							break;
						}
					}
				}else{
					$(currentNotification).fadeOut();
					currentNotification = '';
				}

			} else {
			    //console.log("is NOT in array");
			    var classTemp = $(target).attr('class');
			    switch (classTemp) {
					case 'noHide':
					case 'classNotificationMessenger':
						break;
					default:
						$(currentNotification).fadeOut();
						currentNotification = '';
						break;
				}
			}
            break;
        case 3:
            //alert('Right Mouse button pressed.');
            break;
    }
});

function ajaxReadNotification(type,friend_id){
	var data = {"notification[type]" : type,"notification[friend_id]" : friend_id};
	var url = urlSendAjax +'ReadNotification';
    $.ajax({
        type: 'POST',
        url: url,
        data:data,
        success:function(data){
          	//console.log(data);
        },
        error: function(data) {
            console.log('Gửi dữ liệu thất bại.')
        },
        	dataType:'html'
    });
}

function unreadMessengerClick(type,friend_id){
	if(quantityNotificationMessengerUnRead > 0){
		ajaxReadNotification(type,friend_id);
		$("#divNotificationMessenger"+friend_id).remove();
		--quantityNotificationMessengerUnRead;

		var c = document.getElementById("divQuantityNotificationMessengerUnRead");
		var ctx = c.getContext("2d");
		ctx.clearRect(0, 0, 20, 10);
		if(quantityNotificationMessengerUnRead !=0){
			$('#notificationMessage').css('background-image', 'url("' + urlImg + 'iconNotificationMessengerUnread.png")');
			ctx.fillStyle = "Red";
			ctx.font = "600 10px Arial";
			ctx.textAlign = "right";
			ctx.fillText(quantityNotificationMessengerUnRead,20,10);
		}else{
			$('#notificationMessage').css('background-image', 'url("' + urlImg + 'iconNotificationMessengerRead.png")');
		}
	}
}

function autoCheckNotification(){
	var url = urlSendAjax +'AutoCheckNotification';
	data         = {"quantityUnread[friendship]" : quantityNotificationFriendshipUnRead, "quantityUnread[messenger]": quantityNotificationMessengerUnRead, "quantityUnread[news]": quantityNotificationNewsUnRead};
    $.ajax({
        type: 'POST',
	    url: url,
    	data:data,
        success:function(dataReturn){
        	if(dataReturn!= ''){
        		//console.log(dataReturn);
        		dataReturn = $.parseJSON(dataReturn);
        		console.log(dataReturn);
        		var change = false;
        		if(dataReturn['friendship']!=''){
        			change = true;
        			quantityNotificationFriendshipUnRead = dataReturn['friendship']['quantity'];
					$('#divNotificationFriend').html(dataReturn['friendship']['html']);
        		}
        		if(dataReturn['messenger']!=''){
        			change = true;
        			quantityNotificationMessengerUnRead = dataReturn['messenger']['quantity'];
        			$('#divNotificationMessenger').html(dataReturn['messenger']['html']);
        		}
        		if(dataReturn['news']!=''){
        			change = true;
        			quantityNotificationNewsUnRead  = dataReturn['news']['quantity'];
        			$('#divNotificationNews').html(dataReturn['news']['html']);
        		}
        		if(change){
        			changeReadOrUnread();
        			var snd = new Audio(urlSoundNewMessenger);
					snd.play();
        		}
        	}
        },
        error: function(quantityUnread) {
            console.log('Gửi dữ liệu thất bại.')
        },
            dataType:'html'
    });
}